<template>
  <div class="client-ssoAuthServer-container">
    <el-card shadow="hover">
        <div class="client-ssoAuthServer-search mb15">
            <el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="100px">
            <el-row>                
                <el-col :span="8" class="colBlock">
                  <el-form-item label="授权服务名称" prop="serverName">
                    <el-select filterable v-model="tableData.param.serverName" placeholder="请选择授权服务名称" clearable style="width:200px;">
                        <el-option
                            v-for="dict in sso_server_name"
                            :key="dict.value"
                            :label="dict.label"
                            :value="dict.value"
                        />
                    </el-select>
                  </el-form-item>
                </el-col>                
                <el-col :span="8" class="colBlock">
                  <el-form-item label="创建日期" prop="createdAt">
                    <el-date-picker
                        clearable  style="width: 200px"
                        v-model="tableData.param.createdAt"
                        format="YYYY-MM-DD HH:mm:ss"
                        value-format="YYYY-MM-DD HH:mm:ss"                    
                        type="datetime"
                        placeholder="选择创建日期"                    
                    ></el-date-picker>
                  </el-form-item>
                </el-col>            
                <el-col :span="8" class="colBlock">
                  <el-form-item>
                    <el-button type="primary"   @click="ssoAuthServerList"><el-icon><ele-Search /></el-icon>搜索</el-button>
                    <el-button  @click="resetQuery(queryRef)"><el-icon><ele-Refresh /></el-icon>重置</el-button>
                  </el-form-item>
                </el-col>            
              </el-row>
            </el-form>
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  @click="handleAdd"
                  v-auth="'api/v1/client/ssoAuthServer/add'"
                ><el-icon><ele-Plus /></el-icon>新增</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="success"
                  :disabled="single"
                  @click="handleUpdate(null)"
                  v-auth="'api/v1/client/ssoAuthServer/edit'"
                ><el-icon><ele-Edit /></el-icon>修改</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="danger"
                  :disabled="multiple"
                  @click="handleDelete(null)"
                  v-auth="'api/v1/client/ssoAuthServer/delete'"
                ><el-icon><ele-Delete /></el-icon>删除</el-button>
              </el-col>            
            </el-row>
        </div>
        <el-table v-loading="loading" :data="tableData.data" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />          
          <el-table-column label="主键id" align="center" prop="id"
            min-width="150px"            
             />          
          <el-table-column label="授权服务名称" align="center" prop="serverName" :formatter="serverNameFormat"
            min-width="150px"            
             />          
          <el-table-column label="创建人" align="center" prop="createdBy"
            min-width="150px"            
             />          
          <el-table-column label="创建日期" align="center" prop="createdAt"
            min-width="150px"            
            >
            <template #default="scope">
                <span>{{ proxy.parseTime(scope.row.createdAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
          </el-table-column>          
          <el-table-column align="center" label="应用logo"
            min-width="150px"            
            >
            <template #default="scope">
              <el-image
                style="width: 150px; height: 50px"
                v-if="!proxy.isEmpty(scope.row.logo)"
                :src="proxy.getUpFileUrl(scope.row.logo)"
                fit="contain"></el-image>
            </template>
          </el-table-column>        
          <el-table-column label="操作" align="center" class-name="small-padding" min-width="200px" fixed="right">
            <template #default="scope">            
              <el-button
                type="primary"
                link
                @click="handleView(scope.row)"
                v-auth="'api/v1/client/ssoAuthServer/get'"
              ><el-icon><ele-View /></el-icon>详情</el-button>              
              <el-button
                type="primary"
                link
                @click="handleUpdate(scope.row)"
                v-auth="'api/v1/client/ssoAuthServer/edit'"
              ><el-icon><ele-EditPen /></el-icon>修改</el-button>
              <el-button
                type="primary"
                link
                @click="handleDelete(scope.row)"
                v-auth="'api/v1/client/ssoAuthServer/delete'"
              ><el-icon><ele-DeleteFilled /></el-icon>删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
            v-show="tableData.total>0"
            :total="tableData.total"
            v-model:page="tableData.param.pageNum"
            v-model:limit="tableData.param.pageSize"
            @pagination="ssoAuthServerList"
        />
    </el-card>
    <ApiV1SsoClientSsoAuthServerEdit
       ref="editRef"       
       :serverNameOptions="sso_server_name"       
       :codeTokenTypeOptions="sso_request_type"       
       :refreshTokenTypeOptions="sso_request_type"       
       @ssoAuthServerList="ssoAuthServerList"
    ></ApiV1SsoClientSsoAuthServerEdit>
    <ApiV1SsoClientSsoAuthServerDetail
      ref="detailRef"      
      :serverNameOptions="sso_server_name"      
      :codeTokenTypeOptions="sso_request_type"      
      :refreshTokenTypeOptions="sso_request_type"      
      @ssoAuthServerList="ssoAuthServerList"
    ></ApiV1SsoClientSsoAuthServerDetail>    
  </div>
</template>
<script setup lang="ts">
import {ItemOptions} from "/@/api/items";
import {toRefs, reactive, onMounted, ref, defineComponent, computed,getCurrentInstance,toRaw} from 'vue';
import {ElMessageBox, ElMessage, FormInstance} from 'element-plus';
import {
    listSsoAuthServer,
    getSsoAuthServer,
    delSsoAuthServer,
    addSsoAuthServer,
    updateSsoAuthServer,    
} from "/@/api/sso/client/ssoAuthServer";
import {
    SsoAuthServerTableColumns,
    SsoAuthServerInfoData,
    SsoAuthServerTableDataState,    
} from "/@/views/system/sso/client/ssoAuthServer/list/component/model"
import ApiV1SsoClientSsoAuthServerEdit from "/@/views/system/sso/client/ssoAuthServer/list/component/edit.vue"
import ApiV1SsoClientSsoAuthServerDetail from "/@/views/system/sso/client/ssoAuthServer/list/component/detail.vue"
defineOptions({ name: "apiV1SsoClientSsoAuthServerList"})
const {proxy} = <any>getCurrentInstance()
const loading = ref(false)
const queryRef = ref()
const editRef = ref();
const detailRef = ref();
// 是否显示所有搜索选项
const showAll =  ref(false)
// 非单个禁用
const single = ref(true)
// 非多个禁用
const multiple =ref(true)
const word = computed(()=>{
    if(showAll.value === false) {
        //对文字进行处理
        return "展开搜索";
    } else {
        return "收起搜索";
    }
})
// 字典选项数据
const {    
    sso_server_name,    
    sso_request_type,    
} = proxy.useDict(    
    'sso_server_name',    
    'sso_request_type',    
)
const state = reactive<SsoAuthServerTableDataState>({
    ids:[],
    tableData: {
        data: [],
        total: 0,
        loading: false,
        param: {
            pageNum: 1,
            pageSize: 10,            
            serverName: undefined,            
            createdAt: undefined,            
            dateRange: []
        },
    },
});
const { tableData } = toRefs(state);
// 页面加载时
onMounted(() => {
    initTableData();
});
// 初始化表格数据
const initTableData = () => {    
    ssoAuthServerList()
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
    ssoAuthServerList()
};
// 获取列表数据
const ssoAuthServerList = ()=>{
  loading.value = true
  listSsoAuthServer(state.tableData.param).then((res:any)=>{
    let list = res.data.list??[];    
    list.map((item:any)=>{        
        item.createdBy = item.createdUser?.userNickname        
    })    
    state.tableData.data = list;
    state.tableData.total = res.data.total;
    loading.value = false
  })
};
const toggleSearch = () => {
    showAll.value = !showAll.value;
}
// 授权服务名称字典翻译
const serverNameFormat = (row:SsoAuthServerTableColumns) => {
    return proxy.selectDictLabel(sso_server_name.value, row.serverName);
}
// code换取请求方式字典翻译
const codeTokenTypeFormat = (row:SsoAuthServerTableColumns) => {
    return proxy.selectDictLabel(sso_request_type.value, row.codeTokenType);
}
// 刷新token请求方式字典翻译
const refreshTokenTypeFormat = (row:SsoAuthServerTableColumns) => {
    return proxy.selectDictLabel(sso_request_type.value, row.refreshTokenType);
}
// 多选框选中数据
const handleSelectionChange = (selection:Array<SsoAuthServerInfoData>) => {
    state.ids = selection.map(item => item.id)
    single.value = selection.length!=1
    multiple.value = !selection.length
}
const handleAdd =  ()=>{
    editRef.value.openDialog()
}
const handleUpdate = (row: SsoAuthServerTableColumns|null) => {
    if(!row){
        row = state.tableData.data.find((item:SsoAuthServerTableColumns)=>{
            return item.id ===state.ids[0]
        }) as SsoAuthServerTableColumns
    }
    editRef.value.openDialog(toRaw(row));
};
const handleDelete = (row: SsoAuthServerTableColumns|null) => {
    let msg = '你确定要删除所选数据？';
    let id:number[] = [] ;
    if(row){
    msg = `此操作将永久删除数据，是否继续?`
    id = [row.id]
    }else{
    id = state.ids
    }
    if(id.length===0){
        ElMessage.error('请选择要删除的数据。');
        return
    }
    ElMessageBox.confirm(msg, '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
    })
        .then(() => {
            delSsoAuthServer(id).then(()=>{
                ElMessage.success('删除成功');
                ssoAuthServerList();
            })
        })
        .catch(() => {});
}
const handleView = (row:SsoAuthServerTableColumns)=>{
    detailRef.value.openDialog(toRaw(row));
}
</script>
<style lang="scss" scoped>
    .colBlock {
        display: block;
    }
    .colNone {
        display: none;
    }
    .ml-2{margin: 3px;}
</style>